package com.hp.attendance.service;

import com.hp.attendance.dao.EmployeeDao;
import com.hp.attendance.entity.*;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class EmployeeService {

    private EmployeeDao employeeDao = new EmployeeDao();

    /**
     * 登录校验
     * @param employeeNumber
     * @param password
     */
    public Employee checkLogin(String employeeNumber ,String password) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Employee employee = employeeDao.getEmployee(employeeNumber, password);
        return employee;
    }

    /**
     * 分页查询员工列表
     * @param pageNo 当前页
     * @param pageSize 每页显示的条数
     * @return
     */
    public PageInfo<Employee> getPage(int pageNo , int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        PageInfo<Employee> page = new PageInfo<Employee>();
        //查询分页的数据                           2       10
        List<Employee> list = employeeDao.getLimitList(pageNo, pageSize);
        page.setRecords(list);
        //查询总记录条数
        long total = employeeDao.getTotal();
        page.setTotal(total);
        //设置当前页码
        page.setCurrent(pageNo);
        //设置总页数
        int pages = (int)Math.ceil(total / (pageSize * 1.0));
        page.setPages(pages);
        return page;
    }

    //执行分配角色的功能
    public void dittriRole(List<EmpRole> lister, int empId){
        try {
            //开启事务
            employeeDao.delEmpRole(empId);
            employeeDao.addEmpRole(lister);
            //提交事务
        } catch (Exception throwables) {
            //回滚事务
            throwables.printStackTrace();
        }

    }

    public List<Employee> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        return employeeDao.getList();
    }

}
